<template>
	<view class="content">
		<view class="header" @click="goback">
			<image :src="proxy.serverimageUrl + 'member/back-black.png'" mode=""></image>
		</view>
		<view class="inputtextarea">
			<up-textarea v-model="value1" count placeholder="请输入内容" height="180rpx" maxlength="200"
				border="bottom"></up-textarea>
		</view>
		<view class="uploadbox">
			<up-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:maxCount="9"></up-upload>
		</view>
		<view class="btnbox">
			<u-button type="primary" @click="getrelease(1)" text="发布" color="#2854B7"></u-button>
		</view>
		<up-modal :show="show" @confirm='modalconfirm' @cancel='modalcancel' content='保留此次编辑？' showCancelButton
			confirmText="保留" cancelText="不保留"></up-modal>
	</view>
</template>

<script setup>
	import {
		defineProps,
		getCurrentInstance,
		ref
	} from 'vue'
	import {
		trendsrelease,
		getTrendsid,
		removetid,
		getaccesenTokenData
	} from '../../utils/api.js'
	import {
		onLoad,
		onReady,
		onShow
	} from '@dcloudio/uni-app';
	import BASE_URL from '@/utils/env.js'
	const value1 = ref('')
	const fileList = ref([]);
	const commonFileRequests = ref([]);
	const delCommonFileRequests = ref([]);
	const mid = ref('')
	const show = ref(false)
	const formdata = ref('')
	mid.value = uni.getStorageSync('mid')
	onLoad(() => {
		getTrends(mid.value)
		AToken()
	})
	const AssToken = ref('')
	const AToken = () => {
		getaccesenTokenData().then(res => {
			console.log(res);
			AssToken.value = res.data.accessToken
		})
	}
	const {
		proxy,
		ctx
	} = getCurrentInstance()
	const modalconfirm = () => {
		getrelease(0)
	}
	const modalcancel = () => {
		if (formdata.value) {
			removetid(formdata.value.tid).then(res => {
				/*清空草稿箱*/
			})
		}
		uni.switchTab({
			url: '/pages/square/index'
		})
	}
	const deletePic = (event) => {
		delCommonFileRequests.value.push({
			url: event.file.url,
			type: 0
		})
		fileList.value.splice(event.index, 1);
	};
	const getrelease = (status) => {
		let params = {
			trendsContent: value1.value,
			commonFileRequests: fileList.value,
			delCommonFileRequests: delCommonFileRequests.value,
			mid: uni.getStorageSync('mid'),
			status: status
		}
		const data = {
			content: value1.value
		}
		let url = `https://api.weixin.qq.com/wxa/msg_sec_check?access_token=${AssToken.value}`
		uni.request({
			url: url,
			method: 'POST',
			data: data,
			success(res) {
				if (res.data.errmsg === 'ok') {
					trendsrelease(params).then(res => {
						if (res.code == 200 && status == 1) {
							setTimeout(() => {
								uni.showToast({
									title: '发布成功',
									duration: 1000,
									icon: 'success'
								})
								setTimeout(() => {
									uni.hideToast();
									uni.switchTab({
										url: '/pages/square/index'
									})
								}, 1000)
							}, 0)
						} else {
							uni.switchTab({
								url: '/pages/square/index'
							})
						}
					})
				} else {
					setTimeout(() => {
						uni.showToast({
							title: '内容包含违规信息',
							duration: 1000,
							icon: 'none'
						})
						setTimeout(() => {
							uni.hideToast();

						}, 1000)
					}, 0)

				}
			}
		})

	}
	const goback = () => {
		if (value1.value || fileList.value.length > 1) {
			show.value = true
		} else {
			uni.switchTab({
				url: '/pages/square/index'
			})
		}

	}
	const getTrends = (mid) => {
		getTrendsid(mid).then(res => {
			if (res.code == 200) {
				formdata.value = res.data
				value1.value = res.data.trendsContent
				fileList.value = res.data.commonFileRequests
			}
		})
	}
	// 新增图片
	const afterRead = async (event) => {
		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = fileList.value.length;
		lists.map((item) => {
			fileList.value.push({
				...item,
				status: 'uploading',
				errorMessage: '上传中',
				type: 0
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const result = await uploadFilePromise(lists[i].url);
			let item = fileList.value[fileListLen];
			fileList.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				errorMessage: '',
				url: result,
			});
			fileListLen++;
			console.log(fileList);
		}
	};

	const uploadFilePromise = (url) => {
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: BASE_URL + '/common/upload', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				formData: {
					user: 'test',
				},
				success: (res) => {
					setTimeout(() => {
						const resdata = JSON.parse(res.data)
						console.log(resdata)
						// form.value.avatar = resdata.url
						resolve(resdata.url)
					}, 1000);
				},
			});
		});
	};
</script>

<style lang="scss" scoped>
	.content {
		padding: 114rpx 40rpx 0;
		background-color: #ffffff;

		.header {
			width: 100%;

			image {
				width: 18rpx;
				height: 34rpx;
			}
		}

		.inputtextarea {
			margin-top: 20rpx;
		}

		.uploadbox {
			margin-top: 45rpx;
		}

		.btnbox {
			width: 670rpx;
			height: 110rpx;
			border-radius: 20rpx;
			position: fixed;
			bottom: 80rpx;
		}
	}
</style>